package map04;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
HashMap第二种遍历方式
     Set<Map.Entry<K,V>> entrySet()

     Set：代表entrySet()返回一个Set集合

     Set<>：代表将来存储到Set集合中元素的类型

     Set<Map.Entry>：代表Set集合中存储的元素类型是Map.Entry

     Set<Map.Entry<K,V>>:代表Set集合中存储的元素类型是Map.Entry，Map.Entry指定了键(Key)和值(Value)的类型
 */
public class HashMapDemo02 {
    public static void main(String[] args) {
        //method01();
        HashMap<String, String> hm = new HashMap<>();
        hm.put("无忌", "赵敏");
        hm.put("乔峰", "阿朱");
        hm.put("段誉", "王语嫣");
        Set<Map.Entry<String, String>> entries = hm.entrySet();
        for (Map.Entry<String, String> me:entries)
        {
            System.out.println(me.getKey()+" "+me.getValue() );
        }

    }

    private static void method01() {
        HashMap<String, String> hm = new HashMap<>();
        hm.put("无忌", "赵敏");
        hm.put("乔峰", "阿朱");
        hm.put("段誉", "王语嫣");
        Set<Map.Entry<String, String>> entries = hm.entrySet();
        // System.out.println(entries);

        //获取一个迭代器
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> node = iterator.next();
            System.out.println(node.getKey()+" "+node.getValue());

        }
    }
}
